package com.crm.controller;

import com.crm.base.BaseController;
import com.crm.base.ResultInfo;
import com.crm.exceptions.ParamsException;
import com.crm.model.UserModel;
import com.crm.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public ResultInfo userLogin(String userName,String userPwd){
        ResultInfo resultInfo=new ResultInfo();
            //通过try catch捕获service层的异常，如果service层抛出异常，则表示登录失败，否则登录成功
        try {
            //调用service层登录方法
            UserModel userModel=userService.userLogin(userName,userPwd);
            //设置ResultInfo的result的值（将数据返回给请求）
            resultInfo.setResult(userModel);
        }catch (ParamsException p){
            //失败状态
            resultInfo.setCode(p.getCode());
            resultInfo.setMsg(p.getMsg());
            p.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
            resultInfo.setCode(500);
            resultInfo.setMsg("登录失败");
        }
        return resultInfo;
    }


    @RequestMapping("/queryAllSales")
    @ResponseBody
    public List<Map<String,Object>> queryAllSales(){

        return userService.queryAllSales();
    }
}
